package com.power.using.askapp.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.power.using.askapp.R;
import com.power.using.askapp.base.BaseBackFragment;
import com.power.using.askapp.interfaces.Constant;
import com.power.using.askapp.utils.SPUtil;
import com.power.using.askapp.view.StarView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2019/1/21.
 * Time: 2019/1/21  8:58
 */
public class JudgeFragment extends BaseBackFragment implements View.OnClickListener {


    Unbinder unbinder;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.tv_question_show)
    TextView tvQuestionShow;
    @BindView(R.id.star_view_service)
    StarView starViewService;
    @BindView(R.id.star_view_satisfy)
    StarView starViewSatisfy;
    @BindView(R.id.star_view_solved)
    StarView starViewSolved;
    @BindView(R.id.et_judge_content)
    EditText etJudgeContent;
    @BindView(R.id.bt_judge_commit)
    Button btJudgeCommit;

    private int service_flag = 0;
    private int solved_flag = 0;
    private int setisfy_flag = 0;

    public static JudgeFragment newInstance() {
        Bundle args = new Bundle();
        JudgeFragment fragment = new JudgeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_judge, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initView();
        initListener();
    }

    private void initListener() {
        starViewService.setOnSeletedListener(new StarView.OnSeletedListener() {
            @Override
            public void onSeleted(int value) {
                service_flag = value;
            }
        });
        starViewSatisfy.setOnSeletedListener(new StarView.OnSeletedListener() {
            @Override
            public void onSeleted(int value) {
                setisfy_flag = value;
            }
        });
        starViewSolved.setOnSeletedListener(new StarView.OnSeletedListener() {
            @Override
            public void onSeleted(int value) {
                solved_flag = value;
            }
        });

        tvQuestionShow.setOnClickListener(this);


        btJudgeCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                commitJudgeContent();

            }
        });

//        etJudgeContent.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s.length() > 5) {
//                    s.delete(5, s.length());
//                }
//                int num = 5 - s.length();
//                etJudgeContent.setText(String.valueOf(num));
//
//            }
//        });

    }

    /**
     * 提交评价数据
     */
    private void commitJudgeContent() {


        if(solved_flag==0||setisfy_flag==0||service_flag==0){
            Toast.makeText(getActivity(), "请进行星级评价", Toast.LENGTH_SHORT).show();
            return;
        }

        String et_Content = etJudgeContent.getText().toString().trim();
        if(TextUtils.isEmpty(et_Content)){
            Toast.makeText(getActivity(), "请输入评论内容", Toast.LENGTH_SHORT).show();
            return;
        }

        float everange= Math.round((service_flag+setisfy_flag+solved_flag)*1.0/3);

        Log.i("hashiqi_judge", "commitJudgeContent: "+service_flag+"~~"+setisfy_flag+"~~"+solved_flag+"~~"+et_Content);

        etJudgeContent.setText(everange+"~~~"+service_flag+"~~"+setisfy_flag+"~~"+solved_flag+"~~"+et_Content);


        SPUtil.putString(Constant.ISCLEAR,"1");
        if(TextUtils.isEmpty("a")){
            Toast.makeText(getActivity(), "这是一个toast测试", Toast.LENGTH_SHORT).show();
        }

//          HttpUtils.requestGet(_mActivity, "", new Request(){
//            @Override
//            public void success(String result) {
//                super.success(result);
//            }
//
//            @Override
//            public void error(String error) {
//                super.error(error);
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        showAlertOnlyPos(getActivity(), "对应问题内容的展示,这是一个随意临时值", tvQuestionShow.getText().toString());
    }

    private void initView() {
        toolbarTitle.setText("问题评价");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        cancelLoadingDialog();
    }


}
